package com.samsung.sds.ssa.jsca.editor;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextDoubleClickStrategy;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.IContentAssistant;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;

public class JSPConfiguration extends SourceViewerConfiguration {
	private JSDoubleClickStrategy doubleClickStrategy;
	private JSScanner scanner;
	private ColorManager colorManager;

	public JSPConfiguration(ColorManager colorManager) {
		this.colorManager = colorManager;
	}
	public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
		return new String[]{IDocument.DEFAULT_CONTENT_TYPE, JSPartitionScanner.JS_COMMENT, JSPartitionScanner.JS_KEYWORD, HTMLPartitionScanner.JAVASCRIPT};

	}

	public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
		if (doubleClickStrategy == null)
			doubleClickStrategy = new JSDoubleClickStrategy();
		return doubleClickStrategy;
	}

	protected JSScanner getJAVASCRIPTScanner() {
		System.out.println("JAVASCRIPTSCANNER.getJAVASCRIPTPartitionScanner called");
		if (scanner == null) {
			scanner = new JSScanner(colorManager);
			scanner.setDefaultReturnToken(new Token(new TextAttribute(colorManager.getColor(IJSColorConstants.DEFAULT))));
		}
		return scanner;
	}

	public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
		System.out.println("JSPConfiguration.getPresentationReconciler called");
		final PresentationReconciler reconciler = new PresentationReconciler();

		DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getJAVASCRIPTScanner());
		reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
		reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);

		return reconciler;
	}

	// Content Assist
	public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
		System.out.println("JSPConfiguration.getContentAssistant called!!!");
		final ContentAssistant assistant = new ContentAssistant();
		assistant.setContentAssistProcessor(new MyCompletionProcessor(), IDocument.DEFAULT_CONTENT_TYPE);
		assistant.setContentAssistProcessor(new MyCompletionProcessor(), HTMLPartitionScanner.JAVASCRIPT);

		assistant.enableAutoActivation(true);
		assistant.setAutoActivationDelay(500);
		assistant.setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);

		return assistant;
	}

}
